package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.FloorBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public final class HomeFloorAdapter extends AppAdapter<FloorBean> {

    private FloorBean selected = null;

    public HomeFloorAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvFloorName;

        private ViewHolder() {
            super(R.layout.home_floor_item);
            tvFloorName = findViewById(R.id.tv_floor_name);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(view.isSelected()){
                return;
            }
            view.setSelected(!view.isSelected());
            FloorBean data = (FloorBean) view.getTag();
            selected = data;
            notifyDataSetChanged();
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            FloorBean floor = getItem(position);
            tvFloorName.setText(floor.getFloorName());
            tvFloorName.setTag(floor);
            if("favourite".equals(floor.getFloorSerialNo()) || "common".equals(floor.getFloorSerialNo())){
                tvFloorName.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }else{
                tvFloorName.setCompoundDrawablesWithIntrinsicBounds(AppApplication.getInstance().getThemeDrawable(R.attr.icon_home_floor), null, null, null);
            }
            if(selected != null && selected.getFloorSerialNo().equals(floor.getFloorSerialNo())){
                tvFloorName.setSelected(true);
                tvFloorName.setAlpha(1);
                tvFloorName.setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimensionPixelSize(R.dimen.dp_18));
                tvFloorName.setTypeface(Typeface.DEFAULT_BOLD);
            }else{
                tvFloorName.setSelected(false);
                tvFloorName.setAlpha(0.6f);
                tvFloorName.setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimensionPixelSize(R.dimen.dp_16));
                tvFloorName.setTypeface(Typeface.DEFAULT);
            }
        }
    }

    public FloorBean getSelected() {
        return selected;
    }

    public void checkSelected(FloorBean selected){
        if(selected != null){
            this.selected = selected;
            notifyDataSetChanged();
        }
    }

    public void setSelected(FloorBean selected){
        if(selected != null){
            this.selected = selected;
        }
    }
}